package com.benx.http;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import jodd.http.Cookie;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {

		login();
		//String session = login();

		/*String body = query(session);
		System.out.println(body);1111*/

	}

	public static void regHours() {
		String url = "http://badao.pinganfu.net/my/account";
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("utf8", "✓");
		reqMap.put("authenticity_token",
				"aEfDIlLw4gVB+HgvSTNYHr/cNjBSfXb30qqtnvQNuZokEydEgJcYivwzwCpqqrALxB/MP1tY90izyjTDdc2Cuw==");
		reqMap.put("project_id", "caps");
		reqMap.put("time_entry[issue_id]", "70935");
		reqMap.put("time_entry[spent_on]", "2017-06-22");
		reqMap.put("time_entry[hours]", "3");
		reqMap.put("time_entry[comments]", "");
		reqMap.put("time_entry[activity_id]", "9");
		reqMap.put("continue", "添加并继续");
		// authenticity_token:aEfDIlLw4gVB+HgvSTNYHr/cNjBSfXb30qqtnvQNuZokEydEgJcYivwzwCpqqrALxB/MP1tY90izyjTDdc2Cuw==
		HttpResponse response = HttpRequest.post(url).form(reqMap)
				.cookies(new Cookie("_redmine_session",
						"Y1RJb2FRcXRveENTT1l1b0s0ZmRyYXBJUUZOWjd6VU1EM21sMkJSaTN3WTE0b2prT01BeVRQWUVVbWdJMElhV1JicW9nTWtES3g3K0w2RGZUMElnV0FaQlVvSVpYOFFER1ZkS3ZzQmo3NTRxTWpuZExjYWQ2MFJnVGs1elpPeEZ5Y2F2dEVhdWwyT0xaVmtoSlVEa1k3VUpueE50Vm5XNWpTeWNSUjRmeU5OSm1pR3JBY2F4QXZZNlBDMk5tWTJkL3lEajdscmxYbW1XNFNQWURQdEZLVHo1SUJXZGlYRTI2M2IrKzRYeVZSd05TSmIrYnpUM2lPZFZIUG9SMS9iYi0tT3BHcHlwbzljUXV6VGFoQTQ5T2NQdz09--b4f207e763655e058275bccac85c4ef46afb264b"))
				.send();
		System.out.println(response.bodyText());
	}

	public static String login() {
		
		String url = "http://badao.pinganfu.net/login";
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("username", "xiongjin799");
		reqMap.put("password", "XiongjinA6");
		reqMap.put("authenticity_token", "sa8tMbazMW+qu8AvHF0XJgeDfci6ZT1hDhhWJxtNSZJOXZhGdAwqJpq6VKpmXbEJ0+gxGZ8fWJ5lCKnBj/wEOA==");
		reqMap.put("utf8", "✓");
		reqMap.put("login", "登录 »");
		HttpResponse response = HttpRequest.post(url).form(reqMap).send();
		System.out.println(response.bodyText());

		printCookie(response.cookies());
		return null;
		//return getCookieValue(response.cookies(), "session");
	}

	public static String query(String session) {
		String url = "http://dev.padb.pinganfu.net/sqlsubmit/result.html";
		Map<String, Object> reqMap = new HashMap<String, Object>();

		HttpResponse response = HttpRequest.get(url).cookies(new Cookie("session", session)).send();

		return response.bodyText();
	}

	public static void setProxdy() {
		System.setProperty("http.proxyHost", "10.37.84.36");
		System.setProperty("http.proxyPort", "8080");

		System.setProperty("socksProxyHost", "10.37.84.36");
		System.setProperty("socksProxyPort", "8080");
		System.setProperty("proxySet", "true");
		System.setProperty("proxyHost", "10.37.84.36");
		System.setProperty("proxyPort", "8080");
	}

	public static String getCookieValue(Cookie[] cookies, String key) {
		for (Cookie c : cookies) {
			if (key.equals(c.getName())) {
				return c.getValue();
			}
		}
		return null;
	}

	public static void printCookie(Cookie[] cookies) {
		for (Cookie c : cookies) {
			System.out.println(c.getName() + "=" + c.getValue());
		}
	}
}
